package com.company.recruit.clean.demo2;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * @author wanghengfeng
 * @date 2021年/12月/02日  12:20:56
 */
public class MyPositionReduce extends Reducer<Text,Text, NullWritable,Text> {

    List<String> list=new LinkedList<String>();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        float minSum = 0;
        float maxSum = 0;
        int count = 0;

        for (Text value : values) {
            list.add(value.toString());
            String[] data = value.toString().split("\t");
            if (!(data[0].equals("0.0"))) {
                float min = Float.parseFloat(data[2]);  // +2
                float max = Float.parseFloat(data[3]);
                minSum += min;
                maxSum += max;
                count++;
            }
        }
        float minAve =minSum/count;
        float maxAve =maxSum/count;
//        for (Text v : values) {
//            String[] data = v.toString().split("\t");
//            context.write(new Text("平均工资"),new Text(data[0]+"\t"+data[1]));
//        }
        for (String s : list) {
            try{
                String[] data=s.split("\t");
                if ("0.0".equals(data[2])){
                    context.write(NullWritable.get(),new Text(data[2]+"\t"+data[6]+"\t"+minAve+"\t"+maxAve+"\t"+data[5]+"\t"+data[3]+"\t"+data[4]+"\t"+data[7]+"\t"+data[8]));
                    System.out.println("-----------------------------");
                    System.out.println("data[2]:"+data[2]);
                    System.out.println("data[6]:"+data[6]);
                    System.out.println("-----------------------------");
                }else {
                    context.write(NullWritable.get(),new Text(data[2]+"\t"+data[6]+"\t"+data[0]+"\t"+data[1]+"\t"+data[5]+"\t"+data[3]+"\t"+data[4]+"\t"+data[7]+"\t"+data[8]));
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }

    }
}
